package com.mobile.wizardForm.util;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.ProgressListener;

public class ProgressListenerImpl implements ProgressListener {

private List<String> names=new ArrayList<String>();
	
	private long megaBytes = -1;

	private HttpServletRequest request;
	
	public ProgressListenerImpl( HttpServletRequest request) {
		super();
		this.request = request;
		Enumeration paramNames=request.getParameterNames();
		while(paramNames.hasMoreElements()){
			String name=(String)paramNames.nextElement();
			System.out.println(name);
			this.names.add(name);
		}
		System.out.println(names.size()+">>");
	}

	public ProgressListenerImpl(List<String> names, HttpServletRequest request) {
		super();
		this.names = names;
		this.request = request;
	}

	public List<String> getNames() {
		return names;
	}

	public void setNames(List<String> names) {
		this.names = names;
	}

	public long getMegaBytes() {
		return megaBytes;
	}

	public void setMegaBytes(long megaBytes) {
		this.megaBytes = megaBytes;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	/**
	 * 修改监听状态
	 * @param	pBytesRead			已经读取的数据		
	 * @param	pContentLength		文件的总大小
	 * @param	pItems				表单中的第几个字段
	 */
	public void update(long pBytesRead, long pContentLength, int pItems) {
		// TODO Auto-generated method stub
		/*
		 * 如果names没有初始化化,则返回
		 */

		if(this.names.size()==0){
			return ;
		}
		
		long mBytes = pBytesRead / 1000000;
		if (megaBytes == mBytes&&pBytesRead!=pContentLength) {
			return;
		}
		
		megaBytes = mBytes;
		//System.out.println("We are currently reading item " + pItems);

		if (pContentLength == -1) {
			System.out.println("So far, " + pBytesRead
					+ " bytes have been read.");
		} else {
			setSession(pBytesRead,pContentLength, pItems);
		}
	}

	/**
	 * 把过程百分比放置到session的procss参数中,procss是个Map对象
	 * @param pBytesRead				已经读取的数据
	 * @param pContentLength			文件的总大小
	 * @param pItems					表单中的第几个字段
	 * @param request					请求对象
	 */
	public void setSession(long pBytesRead, long pContentLength, int pItems){
		NumberFormat format = new DecimalFormat("#0.00");
		NumberFormat formatPer = new DecimalFormat("#");
		
		double a = pBytesRead;
		double b = pContentLength;

		double d = Double.valueOf(format.format(a / b));
		double dPer=Double.valueOf(formatPer.format((a / b)*100));
		
		ProgressInfo progressInfo=new ProgressInfo(d,String.valueOf(dPer)+"%");
		
		Enumeration enu=request.getParameterNames();
		
		String fieldName=this.names.get(pItems);
		
		Map<String,ProgressInfo> map=(HashMap<String,ProgressInfo>)this.request.getSession().getAttribute("procss");
		if(map==null){
			Map<String,ProgressInfo> procssMap=new HashMap<String,ProgressInfo>();
			if(pItems==0){
				return;
			}
			procssMap.put(fieldName, progressInfo);
			this.request.getSession().setAttribute("procss", procssMap);
		}else{
			map.put(fieldName, progressInfo);
		}
		/*System.out.println("So far, " + pBytesRead + " of "
				+ pContentLength + " bytes have been read." + progressInfo.getValue()+">>>>"+progressInfo.getText());*/
	}
}
